package kr.co.insoft;

import java.io.Closeable;
import java.io.IOException;
import java.util.Set;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Location;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.keystone.v2_0.features.TenantApi;

import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Module;

public class JCloudTest2 implements Closeable {
	private final ComputeService computeService;

	public static void main(String[] args) throws IOException {
		JCloudTest2 jcloudsNova = new JCloudTest2();

		try {
//			jcloudsNova.listServers();
//			 jcloudsNova.listImages();
			// jcloudsNova.listFlavors();
//			 jcloudsNova.createServer();
			 jcloudsNova.removeServer();
			// jcloudsNova.listKeypair();
			// jcloudsNova.listHardwares();
			// jcloudsNova.listZones();
//			 jcloudsNova.networks();
			jcloudsNova.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jcloudsNova.close();
		}
	}

	public JCloudTest2() {
		Iterable<Module> modules = ImmutableSet
				.<Module> of(new SLF4JLoggingModule());

		String provider = "openstack-nova";
		String identity = "demo:admin"; // tenantName:userName
		String credential = "openstack";
		ComputeServiceContext context = ContextBuilder.newBuilder(provider)
				.endpoint("http://192.168.0.43:5000/v2.0/")
				.credentials(identity, credential).modules(modules)
				.buildApi(ComputeServiceContext.class);

		computeService = context.getComputeService();		
	}

	private void listServers() {
		for (ComputeMetadata computeMetadata : computeService.listNodes()) {
			System.out.println(computeMetadata.toString());
		}
	}

	private void listZones() {
		Set<? extends Location> zones = computeService
				.listAssignableLocations();
		for (Location location : zones) {
			System.out.println("zone info " + location);
		}
	}

	private void listImages() {
		Set<? extends Image> images = computeService.listImages();
//		System.out.println(images);
		for (Image image : images) {
			System.out.println("#################################");
			System.out.println("getId  : "+image.getId());
			System.out.println("getDescription  : "+image.getDescription());
			System.out.println("getBackendStatus  : "+image.getBackendStatus());
			System.out.println("getName  : "+image.getName());
			System.out.println("getProviderId  : "+image.getProviderId());
			System.out.println("getUri  : "+image.getUri());
			System.out.println("getVersion  : "+image.getVersion());
			System.out.println("getOperatingSystem  : "+image.getOperatingSystem().getName());
			System.out.println("getUserMetadata  : "+image.getUserMetadata());			
			System.out.println("#################################");
		}
	}

	private void listHardwares() {
		Set<? extends Hardware> hardwares = computeService
				.listHardwareProfiles();
		for (Hardware hardware : hardwares) {
			System.out.println(hardware.getId() + "," + hardware.getName());
		}
	}

	private void createServer() {
		try {
			TemplateOptions options = new TemplateOptions();
			options.networks("0c9e6135-1ee4-4525-b40c-3bdaa8349223");
			
			Template template = computeService.templateBuilder()
					.locationId("RegionOne").hardwareId("RegionOne/2")
					.imageId("RegionOne/e3d7a338-ec33-4c3d-85de-63f64bca0bc6")
					.options(options)
					.build();
			
			Set<? extends NodeMetadata> nodes = computeService
					.createNodesInGroup("test-openstack", 1, template);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void removeServer() {
		computeService.destroyNode("RegionOne/6782a0e0-0da6-4e6f-9f36-02779d58218a");
				
	}

	
	private void networks() {
		Set<String> networks = computeService.templateOptions().getNetworks();
		System.out.println("networks : "+networks);
	}
	
	public void close() throws IOException {
		Closeables.close(computeService.getContext(), true);
	}
}